home *** CD-ROM | disk | FTP | other *** search
/ MacTech 1 to 12 / MacTech-vol-1-12.toast / Source / MacTech® Magazine / Volume 02 - 1986 / 02.05 May 86.sit / 02.05 May 86 / C May 86 / palette.c / palette.c
Encoding:
C/C++ Source or Header  |  1986-03-30  |  3.7 KB  |  191 lines  |  [TEXT/QED1]

  1. /*
  2.  * Palette List Application
  3.  */
  4.  
  5. #include <event.h>
  6. #include <inits.h>
  7. #include <list.h>
  8. #include <memory.h>
  9. #include <menu.h>
  10. #include <packages.h>
  11. #include <quickdraw.h>
  12. #include <resource.h>
  13. #include <segment.h>
  14. #include <toolutil.h>
  15. #include <window.h>
  16.  
  17. #define MENUBAR 256
  18. #define WINDOW 256
  19. #define ICON 256
  20. #define LDEF 256
  21. #define GRABBER 256
  22.  
  23. #define WIDTH 26
  24. #define HEIGHT 22
  25. #define ROWS 10
  26. #define COLUMNS 2
  27.  
  28. #define POINT(thePt) *(long *)(&thePt)
  29.  
  30. WindowPtr palette;
  31.  
  32. main()
  33.     {
  34.     MacInit();
  35.     MenuInit();
  36.     PaleInit();
  37.     for (;;)
  38.         EventDoOne();
  39.     }
  40.  
  41. MacInit()
  42.     {
  43.     InitGraf(&thePort);
  44.     InitFonts();
  45.     InitWindows();
  46.     InitMenus();
  47.     TEInit();
  48.     InitDialogs(0l);
  49.     FlushEvents(everyEvent, 0);
  50.     InitCursor();
  51.     }
  52.  
  53. MenuInit()
  54.     {
  55.     SetMenuBar(GetNewMBar(MENUBAR));
  56.     DrawMenuBar();
  57.     }
  58.  
  59. pascal void IconListDef(lMessage, lSelect, lRect, lCell, lDataOffset, lDataLen, lHandle)
  60.     int lMessage;
  61.     int lSelect;
  62.     Rect *lRect;
  63.     long lCell;
  64.     int lDataOffset;
  65.     int lDataLen;
  66.     ListHandle lHandle;
  67.     {
  68.     char tag;
  69.     BitMap iconBits;
  70.     
  71.     BlockMove(lRect, &iconBits.bounds, (long) sizeof(Rect));
  72.     switch (lMessage)
  73.         {
  74.         case lInitMsg:
  75.             break;
  76.             
  77.         case lDrawMsg:
  78.             if (lDataLen)
  79.                 {
  80.                 tag = *(char *) (*lHandle)->cells;
  81.                 HLock((*lHandle)->cells);
  82.                 iconBits.baseAddr = *(*lHandle)->cells + lDataOffset;
  83.                 iconBits.rowBytes = 4;
  84.                 CopyBits(&iconBits, &thePort->portBits, lRect, lRect, srcCopy, 0l);
  85.                 *(char *) (*lHandle)->cells = tag;
  86.                 }
  87.             else
  88.                 EraseRect(lRect);
  89.             if (!*(char *) &lSelect)
  90.                 break;
  91.             
  92.         case lHiliteMsg:
  93.             iconBits.bounds.right--; iconBits.bounds.bottom--;
  94.             InvertRect(&iconBits.bounds);
  95.             break;
  96.             
  97.         case lCloseMsg:
  98.             break;
  99.         }
  100.     }
  101.  
  102. PaleInit()
  103.     {
  104.     Handle theLDEF;
  105.     Rect theView;
  106.     Rect theBounds;
  107.     int scroll;
  108.     Point theSize;
  109.     ListHandle theList;
  110.     Cell theCell;
  111.     int id;
  112.     Handle theIcon;
  113.     
  114.     palette = GetNewWindow(WINDOW, 0l, -1l);
  115.  
  116.     theLDEF = GetResource('LDEF', LDEF);
  117.     *(long *) (*theLDEF + 2) = (long) &IconListDef;
  118.     
  119.     SetRect(&theBounds, 0, 0, COLUMNS, ROWS);
  120.     BlockMove(&palette->portRect, &theView, (long) sizeof(Rect));
  121.     if (scroll = (theView.bottom - theView.top) / HEIGHT < ROWS)
  122.         theView.right -= 15;
  123.     SetPt(&theSize, WIDTH, HEIGHT);
  124.     theList = LNew(&theView, &theBounds, POINT(theSize), LDEF, palette, FALSE, FALSE, FALSE, scroll ? TRUE : FALSE);
  125.     SetWRefCon(palette, theList);
  126.     (*theList)->selFlags = lOnlyOne;
  127.  
  128.     for (id = 0; id < COLUMNS * ROWS; id++)
  129.         {
  130.         SetPt(&theCell, id % COLUMNS, id / COLUMNS);
  131.         HLock(theIcon = GetIcon(id + ICON));
  132.         LSetCell(*theIcon, 128, POINT(theCell), theList);
  133.         HUnlock(theIcon);
  134.         }
  135.  
  136.     SetPt(&theCell, 0, 0);
  137.     LSetSelect(TRUE, POINT(theCell), theList);
  138.     
  139.     LDoDraw(TRUE, theList);
  140.     ShowWindow(palette);
  141.     }
  142.  
  143. EventDoOne()
  144.     {
  145.     EventRecord theEvent;
  146.     WindowPtr theWindow;
  147.     
  148.     if (GetNextEvent(everyEvent, &theEvent))
  149.         {
  150.         switch (theEvent.what)
  151.             {
  152.             case mouseDown:
  153.                 switch (FindWindow(POINT(theEvent.where), &theWindow))
  154.                     {
  155.                     case inMenuBar:
  156.                         if (MenuSelect(POINT(theEvent.where)))
  157.                             ExitToShell();
  158.                         break;
  159.                         
  160.                     case inContent:
  161.                         if (theEvent.modifiers & optionKey)
  162.                             {
  163.                             SetCursor(*GetCursor(GRABBER));
  164.                             LActivate(FALSE, GetWRefCon(palette));
  165.                             DragWindow(palette, POINT(theEvent.where), &screenBits.bounds);
  166.                             LActivate(TRUE, GetWRefCon(palette));
  167.                             InitCursor();
  168.                             }
  169.                         else
  170.                             {
  171.                             SetPort(palette);
  172.                             GlobalToLocal(&theEvent.where);
  173.                             LClick(POINT(theEvent.where), theEvent.modifiers, GetWRefCon(palette));
  174.                             }
  175.                         break;
  176.                     }
  177.                 break;
  178.                 
  179.             case updateEvt:
  180.                 BeginUpdate(palette);
  181.                 LUpdate(palette->visRgn, GetWRefCon(palette));
  182.                 EndUpdate(palette);
  183.                 break;
  184.                 
  185.             case activateEvt:
  186.                 LActivate(theEvent.modifiers & activeFlag ? TRUE : FALSE, GetWRefCon(palette));
  187.                 break;
  188.             }
  189.         }
  190.     }
  191.